/**
 * Por Joshua
 * Componente que encapsula un dataview que cargar su store mientras el usuarios baja el scroll
 * @class Extmx.PanelView
 *
 * @extends Ext.Container
 * @autor Ing. Josue Hernandez Gomez<br>
 *        joshua@extjs.mx<br>
 *        http://www.joshua.com.mx
 * @fecha 12/2/12 5:36 PM. Mexico DF
 */


Ext.ns("Extmx.PanelView");
Extmx.PanelView=Ext.extend(Ext.Container,{
    loadOnRender: true,
    frame        : false,
    border:false,
    singleSelect: true,//seleccionar solo de uno en uno
    layout:'fit',

    style:'background:#FFF;',
    autoScroll: true,

    initComponent: function (){
        this.store=this.store ||  this.getStore();

        this.dv= new Extmx.DataView({
            loadOnRender: this.loadOnRender,
            style:'overflow:auto; ',
            emptyText :this.emptyText ,
            singleSelect: this.singleSelect,
            multipleSelect: this.multipleSelect,
            store: this.store,
            tpl: this.tpl   ,
            itemSelector :this.itemSelector,
            overClass:this.overClass,
            clase:this.clase,
            metodo:this.metodo,
            basepath:this.basepath
        });

        this.store= this.dv.store;

        this.items= this.dv;

        Extmx.PanelView.superclass.initComponent.apply(this,arguments);

        this.relayEvents(this.dv.store, [ "load","beforeload", "click"]);

        this.addEvents("rowclick");

        this.dv.on({
            scope: this,
            click: function (){
                this.fireEvent("rowclick", this, this.getSeleccionado());
            },
            afterrender: function (d){
                d.el.on({
                    scope: this//,
                    //scroll:  this.cargarMasStore.createDelegate(this,[d],false)
                });
            }
        });

        this.on({
            scope: this,
            
            render: function (){
                if(this.loadOnRender)
                    this.store.load();
            }
        });
    },

    cargarMasStore: function (d){
        //log("eSCROLLEADO!!!");
        //log(d.height);
        log(d.el);

        log(d.el.dom.scrollHeight);
        log(d.el.getScroll() );

        if(d.el.dom.scrollHeight-1000 <d.el.getScroll().top )
            this.store.load({
                add: true
            });
    },

    getSeleccionado: function (){
        return this.dv.getSeleccionado();
    },

    getSeleccionados: function (){
        return this.dv.getSeleccionados();
    },

    hasSelection: function (){
        return this.dv.hasSelection();
    },

    load: function (){
        return this.dv.load();
    },

    reload: function (){
        return this.dv.reload();
    }
});